# -*- coding: utf-8 -*-
"""
用于读取的组件
"""

import os
import ppc.pipe
import ppc.extensions.url


class DirectoryReader(ppc.pipe.Pipe):
    """目录读取"""
    def handle(self):
        input = self.input if self.input else self['directory'].value

        for (cur, dirs, files) in os.walk(input):
            for d in dirs:
                self.transfer({
                    'type': 'D',
                    'name': d,
                    'path': os.path.join(cur, d)})
            for f in files:
                self.transfer({
                    'type': 'F',
                    'name': f,
                    'path': os.path.join(cur, f)})

            if self['recursive'].value != 'yes':
                break

            # 是否循环中止
            if self.flag == ppc.pipe.Flag.Break:
                self.flag = ppc.pipe.Flag.Normal
                break


class UrlReader(ppc.pipe.Pipe):
    """URL读取"""
    def handle(self):
        # 地址
        url = self['url'].value

        # 获取到数据是否解码为文本
        decode = True
        if 'format' in self and self['format'].value == 'binary':
            decode = False

        # 是否有请求的headers
        headers = {}
        if 'headers' in self:
            lines = self['headers'].value.split("\n")
            for line in lines:
                line = line.strip()
                if line != '':
                    k, v = line.split(':')
                    headers[k] = v

        # 请求
        html = ppc.extensions.url.request(url, decode = decode, headers = headers)

        # 传递
        if html:
            self.transfer(html)
